﻿Public Class frmGestionMarcasVehiculos
    Dim utilidades As New Utilidades
    Dim _formPrincipal As frmPrincipal

    Public Sub New(ByRef form As frmPrincipal)
        Me.InitializeComponent()
        _formPrincipal = form
    End Sub

    Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click
        Me.Close()
    End Sub

    Private Sub frmGestionMarcasVehiculos_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        utilidades.confirmarSalirDelFormulario(e)
    End Sub

    Private Sub frmGestionMarcasVehiculos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myAccesoDatos As New AccesoDatos

        'Ejecuto una Búsqueda sin Filtros para llenar la Grilla
        Me.buscar("")
    End Sub

    Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click
        Me.buscar(armarWhereConsulta)
    End Sub

    Public Sub buscar(ByVal where As String)
        Dim myAccesoDatos As New AccesoDatos
        Dim myConsulta As String

        myConsulta = _
            "SELECT ID, DESCRIPCION FROM MARCAS_VEHICULOS " & _
            "WHERE 1 = 1 "

        If Not where.Trim = "" Then
            myConsulta = myConsulta & where
        End If

        myConsulta = myConsulta & " ORDER BY DESCRIPCION"

        Me.gridMarcas.DataSource = myAccesoDatos.ejecutarConsulta(myConsulta)

        Me.habilitarBotonesSegunGrilla()

    End Sub

    Public Function armarWhereConsulta() As String
        Dim where As New String("")
        
        If Not (txtMarca.Text().Trim() = "") Then
            where = where & " AND UPPER(DESCRIPCION) LIKE UPPER('%" & txtMarca.Text().Trim() & "%')"
        End If

        Return where

    End Function


    Public Sub habilitarBotonesSegunGrilla()
        'Dependiendo si la Grilla tiene resultados habilito los botones de Edicion y Borrado
        If Me.gridMarcas.RowCount < 1 Then
            Me.cmdModificar.Enabled = False
            Me.cmdBorrar.Enabled = False
        Else
            Me.cmdModificar.Enabled = True
            Me.cmdBorrar.Enabled = True
        End If
    End Sub

    Private Sub cmdBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBorrar.Click
        If MessageBox.Show("¿Está seguro que desea eliminar esta Marca de Vehículo?", "Importante", _
        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then

            Dim myAccesoDatos As New AccesoDatos
            Dim filaSeleccionada As DataGridViewRow
            Dim idMarca As New String("")
            For Each filaSeleccionada In gridMarcas.SelectedRows
                idMarca = filaSeleccionada.Cells(0).Value
            Next

            Dim myConsulta As String
            Dim tabla As Data.DataTable
            myConsulta = "SELECT ID_MARCA FROM BIENES_MUEBLES WHERE ID_MARCA = " & idMarca
            tabla = myAccesoDatos.ejecutarConsulta(myConsulta)

            If tabla.Rows.Count = 0 Then
                myConsulta = "DELETE FROM MARCAS_VEHICULOS WHERE ID = " & idMarca
                myAccesoDatos.ejecutarConsulta(myConsulta)
                Me.buscar(armarWhereConsulta)
            Else
                MsgBox("La Marca se encuentra asociada a Bienes Muebles. No es posible su eliminación.", vbOKOnly + vbCritical, "Atención")
            End If
        End If

    End Sub

    Private Sub cmdModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModificar.Click
        Dim filaSeleccionada As DataGridViewRow
        Dim idMarca As Long
        For Each filaSeleccionada In gridMarcas.SelectedRows
            idMarca = filaSeleccionada.Cells(0).Value
        Next

        Dim frmMarcaVehiculo As New frmMarcaVehiculo(Constantes.Ventana_Tipo_Apertura_Edicion, _formPrincipal)
        frmMarcaVehiculo.idMarca = idMarca
        frmMarcaVehiculo.ShowDialog()
    End Sub

    Private Sub cmdNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNuevo.Click
        Dim frmMarcaVehiculo As New frmMarcaVehiculo(Constantes.Ventana_Tipo_Apertura_Nuevo, _formPrincipal)
        frmMarcaVehiculo.ShowDialog()
    End Sub
End Class